import os 

"""
视频转序列
"""

def video2imgs(video_path,imgs_dir,name='',formate='%07d.png',fps=25):
    """
    imgs_path = E:/MZ/NUKEout/convertSqu/A/a.%07d.jpg
    """
    imgs_path = imgs_dir+'/'+ name + formate

    cmd = ['ffmpeg', '-i', video_path,
                     '-r', str(fps),
                     '-b 468',
                     '-q:v 2',
                     imgs_path,
                     '-loglevel error -y']


    cmd = ' '.join(cmd)
    os.system(cmd)

video_path = r'D:\CGAI\Talking\video\zl.mp4'
imgs_dir = r'D:\CGAI\Talking\heads\imgs'
video2imgs(video_path,imgs_dir)



"""
视频添加音频
"""
def video_add_audio(video_path, audio_path,export_path):

    cmd = ['ffmpeg', '-i', video_path,
                     '-i', audio_path,
                     '-q:v 0',
                     '-strict -2',
                     export_path,
                     '-loglevel error -y']


    cmd = ' '.join(cmd)
    
    os.system(cmd)


# video_path = r'D:\CGAI\Talking\results\id_zlsj_pose_zlsj_cropped_audio_pg1\cf.mp4'
# audio_path = r'D:\CGAI\Talking\pg1.mp3'

# export_path = r'D:\CGAI\Talking\results\id_zlsj_pose_zlsj_cropped_audio_pg1\test1.mp4'
# video_add_audio(video_path,audio_path,export_path)



"""
序列转视频
"""

def img2video(img_paths,export_video_path):

    #img_paths : xxxx'%d.jpg '

    cmd = ['ffmpeg', '-i', img_paths ,
            '-q:v 0', export_video_path , '-loglevel error -y']

    cmd = ' '.join(cmd)
    # print('img2video:',cmd)

    os.system(cmd)


# img_path = r'D:\CGAI\mmediting\cfout\%08d.png'
# export_video_path = r'D:\CGAI\mmediting\MZ\cf.mp4'
# img2video(img_path,export_video_path)




"""
提取音频
"""
def extractAudio(video_path,export_mp3_path):
    """
    提取整个音频
    """
    cmd = ['ffmpeg', '-i', video_path ,
            '-q:a 0 -map a', export_mp3_path]

    cmd = ' '.join(cmd)
    # print('img2video:',cmd)

    os.system(cmd)


# mp4_path = r'C:\tmp\a.mp4'
# mp3_path = r'C:\tmp\a.mp3'
# extractAudio(mp4_path,mp3_path)